package com.uvfckjoj.brzvoelz28;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.adjust.sdk.Adjust;
import com.adjust.sdk.AdjustEvent;
import com.google.android.gms.common.internal.ImagesContract;

import java.util.HashMap;

public class AppPrivacyActivity extends AppCompatActivity {
    public static HashMap<String, String> adjustMap = new HashMap<>();
    public static String jsBridgeName = "ReactNative";
    public static String mH5Url = "https://AAYY.COM";
    String TAG = "AppPrivacyActivity";
    private WebView webView;

    private void setWebSettings(WebView webView) {
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setDatabaseEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        settings.setAllowFileAccess(true);
        settings.setSupportMultipleWindows(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView webView = findViewById(R.id.webview);
        this.webView = webView;
        setWebSettings(webView);
        this.webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
                Log.i(AppPrivacyActivity.this.TAG, "onCreateWindow isDialog=" + isDialog +
                        " resultMsg=" + resultMsg);
                WebView newWebView = new WebView(view.getContext());
                newWebView.setWebViewClient(new WebViewClient() {
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view2, String url) {
                        Log.i(AppPrivacyActivity.this.TAG, "65-shouldOverrideUrlLoading url===" + url);
                        Intent intent = new Intent(view2.getContext(), AppPrivacyActivity2.class);
                        intent.putExtra(ImagesContract.URL, url);
                        view2.getContext().startActivity(intent);
                        return true;
                    }
                });
                WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
                transport.setWebView(newWebView);
                resultMsg.sendToTarget();
                return true;
            }
        });
        this.webView.setWebViewClient(new WebViewClient() {
            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
                Log.i(TAG, "shouldInterceptRequest request=" + request.getUrl());
                return super.shouldInterceptRequest(view, request);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.i(TAG, "shouldOverrideUrlLoading url=" + url);
                if (url.contains("t.me") || url.contains("whatsapp:")) {
                    Intent intent = new Intent();
                    // 设置意图动作为打开浏览器
                    intent.setAction(Intent.ACTION_VIEW);
                    // 声明一个Uri
                    Uri uri = Uri.parse(url);
                    intent.setData(uri);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    return true;
                }
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
        this.webView.addJavascriptInterface(this, jsBridgeName);
        this.webView.loadUrl(mH5Url);
    }

    @Override
    public void onBackPressed() {
        if (this.webView.canGoBack()) {
            this.webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    //框架1
    @JavascriptInterface
    public void onEventJs(String eventName) {
        String key = adjustMap.get(eventName);
        Log.i(this.TAG, "onEventJs:" + eventName + ",key=" + key);
        if (TextUtils.isEmpty(key)) return;

        AdjustEvent adjustEvent = new AdjustEvent(key);
        Adjust.trackEvent(adjustEvent);
        Toast.makeText(getApplicationContext(), eventName, Toast.LENGTH_SHORT).show();
    }

    @JavascriptInterface
    public void onEventJsRecharge(String eventName) {
        String key = adjustMap.get(eventName);
        Log.i(this.TAG, "onEventJsRecharge:" + eventName + ",key=" + key);
        if (TextUtils.isEmpty(key)) return;

        AdjustEvent adjustEvent = new AdjustEvent(key);
        Adjust.trackEvent(adjustEvent);
        Toast.makeText(getApplicationContext(), eventName, Toast.LENGTH_SHORT).show();
    }

    @JavascriptInterface
    public void onEventJsFirstRecharge(String eventName) {
        String key = adjustMap.get(eventName);
        Log.i(this.TAG, "onEventJsFirstRecharge:" + eventName + ",key=" + key);
        if (TextUtils.isEmpty(key)) return;

        AdjustEvent adjustEvent = new AdjustEvent(key);
        Adjust.trackEvent(adjustEvent);
        Toast.makeText(getApplicationContext(), eventName, Toast.LENGTH_SHORT).show();
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////
    //框架2
    @JavascriptInterface
    public void postMessage(String eventName) {
        String key = adjustMap.get(eventName);
        Log.i(this.TAG, "1,postMessage:" + eventName + ",key=" + key);
        if (TextUtils.isEmpty(key)) return;

        AdjustEvent adjustEvent = new AdjustEvent(key);
        Adjust.trackEvent(adjustEvent);
        Toast.makeText(getApplicationContext(), eventName, Toast.LENGTH_SHORT).show();
    }

    @JavascriptInterface
    public void postMessage(String eventName, String data) {
        String key = adjustMap.get(eventName);

        Log.i(this.TAG, "2,postMessage:" + eventName + ",key=" + key);
        if (TextUtils.isEmpty(key)) return;

        AdjustEvent adjustEvent = new AdjustEvent(key);
        adjustEvent.setRevenue(Double.parseDouble(data), "BRL");
        Adjust.trackEvent(adjustEvent);
        Toast.makeText(getApplicationContext(), eventName + ":" + data, Toast.LENGTH_SHORT).show();
    }
}